﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Master.Validator;
using RockCompany.Master.View;
using RockCompany.Common.Forms;

namespace RockCompany.Master.View
{
    public partial class SI02A : ParentFrm
    {
        #region Properties
        private Translator trans;
        private SI02AFormModel frmModel;
        private DataGridViewRow selectRow;
        private DataTable matOrderDetails;
        private bool isPassed = false;
        private bool isNew = true;
        private string sUserName; // login user
        private SW01B matsWorksManage;
        #endregion

        #region Constructor
        public SI02A(Translator translator, string sUName, Form caller)
            : base(sUName, caller)
        {
            // Initial forms' components
            Owner = caller;
            sUserName = sUName;
            trans = translator;
            InitializeComponent();
        }
        #endregion

        #region Event Methods
        private void radioButton_Pass_CheckedChanged(object sender, EventArgs e)
        {
            isPassed = true;
        }

        private void radioButton_Unpass_CheckedChanged(object sender, EventArgs e)
        {
            isPassed = false;
        }

        private void dataGridView_MatOrderDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView_MatOrderDetail.CurrentCell != null && dataGridView_MatOrderDetail.CurrentCell.ColumnIndex == 0 && e.RowIndex > -1)
                frmModel.addRemoveCheckedList(0, frmModel.getCheckedRow(e.RowIndex));
        }

        private void dataGridView_MatOrderDetail_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int num1_index = dataGridView_MatOrderDetail.CurrentRow.Cells[PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH].ColumnIndex;
            int num2_index = dataGridView_MatOrderDetail.CurrentRow.Cells[PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH].ColumnIndex;
            if (e.ColumnIndex == num1_index || e.ColumnIndex == num2_index)
            {
                string s = UserValidator.isValidNumer(e.FormattedValue);
                e.Cancel = !string.IsNullOrEmpty(s);
                if (e.Cancel)
                {
                    InforDialog.showWarningrBox(Owner, trans.getMessageCaption(s), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_NewWorkOrder_Click(object sender, EventArgs e)
        {
            //SI04 inventory = new SI04(trans, sUserName, this);
            //inventory.callChooser(SI04Const.NO_MAT_TYPE);
            //if (inventory.ShowDialog(this) == DialogResult.OK)
            //{
            //    DataTable selectedData = inventory.getSelectedMatsNo();
            //    inventory.Dispose();
            //    if (frmModel.addNewMatOrderDetail(sUserName, selectedWorkOrder))
            //        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
            //    else
            //        InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            //}
            matsWorksManage = new SW01B(trans, sUserName, this);
            matsWorksManage.callChooser(true);
            if (matsWorksManage.ShowDialog(this) == DialogResult.OK)
            {
                List<string> selectedWorkOrder = matsWorksManage.getSelectedWO();
                matsWorksManage.Dispose();
                if (frmModel.addNewMatOrderDetail(sUserName, selectedWorkOrder))
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void button_AddNewVtu_Click(object sender, EventArgs e)
        {
            SI01 newVtu = new SI01(trans, sUserName, this);
            newVtu.callChooser(SI01Const.callChooseVTU);
            if (newVtu.ShowDialog(this) == DialogResult.OK)
            {
                DataTable selectedVal = newVtu.getSelectedMatsNo();
                newVtu.Dispose();
                if (frmModel.addNewNoneMatList(sUserName, selectedVal))
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));

            }
        }

        private void button_DelMatOrderDetail_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                if (frmModel.removeMatOredrDetail(textBox_NewMatOrderNumber.Text))
                {
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                }
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void button_SaveMatOrder_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_ADD)))
            {
                if (!string.IsNullOrEmpty(textBox_NewMatOrderNumber.Text))
                {
                    PHIEU_LANH_NVL_DTO curMatOrder = new PHIEU_LANH_NVL_DTO();
                    curMatOrder.MS_PHIEU_LANH_NVL = textBox_NewMatOrderNumber.Text;
                    curMatOrder.NGUOI_LANH_NVL = textBox_NguoiLanh.Text;
                    curMatOrder.THU_KHO = textBox_ThuKHo.Text;
                    curMatOrder.NGAY_LANH_NVL = dateTimePicker_NgayLanh.Value;
                    curMatOrder.PHE_DUYET = isPassed;
                    curMatOrder.CREATION_USER = (string.IsNullOrEmpty(sUserName) ? "" : sUserName);

                    bool b1 = false, b2 = false;
                    if (!isNew)
                    {
                        b1 = frmModel.updateMatOrder(curMatOrder);
                        b2 = frmModel.saveData(textBox_NewMatOrderNumber.Text, (string.IsNullOrEmpty(sUserName) ? "" : sUserName));
                        if (b1 && b2)
                        {
                            InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.UpdateComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                        }
                    }
                    else
                    {
                        b1 = (dataGridView_MatOrderDetail.Rows.Count > 0 && frmModel.insertNewMatOrder(curMatOrder));
                        if (b1)
                            b2 = frmModel.saveData(textBox_NewMatOrderNumber.Text, (string.IsNullOrEmpty(sUserName) ? "" : sUserName));
                        if (b1 && b2)
                        {
                            InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                            isNew = false;
                            textBox_NewMatOrderNumber.Enabled = false;
                        }
                        else
                            InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                    }
                }
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.REQUIRED_FIELD), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));

            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Utility Methods
        public void initExistData(DataGridViewRow matOrderRow, DataTable matOrderDetails)
        {
            selectRow = matOrderRow;
            this.matOrderDetails = matOrderDetails;
            isNew = false;
        }

        public void initDefaultData()
        {
            frmModel = new SI02AFormModel();
            frmModel.Owner = this;
            trans.controlTranslate(this);
            frmModel.translator = this.trans;

            PHIEU_LANH_NVL_CT_DTO temp = new PHIEU_LANH_NVL_CT_DTO();
            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_CHON));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_SO_LO));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_CREATION_USER));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_CREATION_TIME));
            List<string> readOnlyCols = new List<string>();
            readOnlyCols.Add(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU);
            readOnlyCols.Add(PHIEU_LANH_NVL_CT_DTO.N_SO_LO);
            frmModel.buildDataGrid(dataGridView_MatOrderDetail, properties, readOnlyCols);

            if (!isNew)
            {
                textBox_NewMatOrderNumber.Text = selectRow.Cells[PHIEU_LANH_NVL_DTO.N_MS_PHIEU_LANH_NVL].EditedFormattedValue.ToString();
                textBox_NewMatOrderNumber.ReadOnly = !isNew;
                textBox_NguoiLanh.Text = selectRow.Cells[PHIEU_LANH_NVL_DTO.N_NGUOI_LANH_NVL].EditedFormattedValue.ToString();
                textBox_ThuKHo.Text = selectRow.Cells[PHIEU_LANH_NVL_DTO.N_THU_KHO].EditedFormattedValue.ToString();
                dateTimePicker_NgayLanh.Value = Util.parseDateTime(selectRow.Cells[PHIEU_LANH_NVL_DTO.N_NGAY_LANH_NVL].EditedFormattedValue);
                radioButton_Pass.Checked = (bool)selectRow.Cells[PHIEU_LANH_NVL_DTO.N_PHE_DUYET].EditedFormattedValue;
                radioButton_Unpass.Checked = !(bool)selectRow.Cells[PHIEU_LANH_NVL_DTO.N_PHE_DUYET].EditedFormattedValue;
                isPassed = (bool)selectRow.Cells[PHIEU_LANH_NVL_DTO.N_PHE_DUYET].EditedFormattedValue;

                frmModel.newMatOredrDetails = matOrderDetails;
            }
            else
            {
                frmModel.newMatOredrDetails = frmModel.buildEmptyDataTable(properties);
            }
            dataGridView_MatOrderDetail.DataSource = frmModel.newMatOredrDetails;
        }
        #endregion
    }
}
